package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter.DailyAdapter;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter.QuestionAdapter;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.adapter.ShareAdapter;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.Daily;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.Question;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.DailyContent;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.entity.share.Share;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share.ShareActivity;
import com.coffeeloversupportlibrary.core.base.BaseFragment;
import com.coffeeloversupportlibrary.databinding.DailyShareFragmentBinding;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/14 0:11
 * @projectName CoffeeLoverSupportLibrary
 * @className DailyShareFragment.java
 * @description <com.google.android.material.tabs.TabLayout
 *                 android:id="@+id/tabs"
 *                 android:layout_width="match_parent"
 *                 android:layout_height="wrap_content"
 *                 app:tabIndicatorColor="#ADBE107E"
 *                 app:tabMode="scrollable"
 *                 app:tabTextColor="@color/purple_200"
 *                 app:tabSelectedTextColor="@color/black"
 *                 app:tabGravity="center"
 *               />   颜色设置出现问题的原因 app 命名空间
 *               todo:初次调用 viewmodel init 方法创建相应的 Adapter 保持数据的解决方案 先对 view
 *               todo:不考虑 页面分享 Fragment间的切换 状态保存（setAdapter导致丢失状态）
 */
public class DailyShareFragment extends BaseFragment<DailyShareViewModel, DailyShareFragmentBinding> {
    // TODO: 2021/11/24  仿照首页思路   Fragment 持有 Adapter 被观察数据改为数据集合  setAdapter会刷新 RecyclerView

    private DailyAdapter dailyAdapter;
    private QuestionAdapter questionAdapter;
    private ShareAdapter shareAdapter;


    @Override
    public void registerUIChangeCallBack() {
        getViewModel().getDaily().observe(this, new Observer<List<DailyContent>>() {
            @Override
            public void onChanged(List<DailyContent> dailies) {
                 if(null!=dailies){
                     dailyAdapter.setDataSource(dailies);
                     getBinding().shareView.setLayoutManager(new LinearLayoutManager(getContext()));
                     getBinding().shareView.setAdapter(dailyAdapter);
                 }
            }
        });

        getViewModel().getShares().observe(this, new Observer<List<Share>>() {
            @Override
            public void onChanged(List<Share> shareContents) {
                if(null!=shareContents){
                    shareAdapter.setDataSource(shareContents);
                    getBinding().shareView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                    getBinding().shareView.setAdapter(shareAdapter);
                }

            }
        });

       getViewModel().getQuestions().observe(this, new Observer<List<Question>>() {
           @Override
           public void onChanged(List<Question> questions) {
               if(null!=questions){
                   questionAdapter.setDataSource(questions);
                   getBinding().shareView.setLayoutManager(new LinearLayoutManager(getContext()));
                   getBinding().shareView.setAdapter(questionAdapter);
               }
           }
       });

    }

    @Override
    public int getLayoutId() {
        return R.layout.daily_share_fragment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //初始化 Adapter
        initAdapter();
        //初始化 TabLayout
        getBinding().shareTab.addTab(getBinding().shareTab.newTab().setText("daily"));
        getBinding().shareTab.addTab(getBinding().shareTab.newTab().setText("share"));
        getBinding().shareTab.addTab(getBinding().shareTab.newTab().setText("question"));
        //tab 和数据变化联动--- UI 回调---tab 控制UI 切换
        getBinding().shareTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition()==0){
                   getViewModel().loadDaily();
                }else if(tab.getPosition()==1){
                   getViewModel().loadShare();
                }else if(tab.getPosition()==2){
                   getViewModel().loadQuestion();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //初次展示首页
        getViewModel().loadDaily();
        //分享功能监听
        getBinding().openShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ShareActivity.class);
            }
        });
    }
    private void initAdapter(){
        this.dailyAdapter=new DailyAdapter(null);
        this.questionAdapter=new QuestionAdapter(null);
        this.shareAdapter=new ShareAdapter(null);
    }


    // TODO: 2021/11/27 重写该方法 实现 动态更新时的刷新
    @Override
    public void onResume() {
        super.onResume();
        //分享结束回到该页面  刷新动态
        getViewModel().loadDaily();
       //2021-11-27 13:58:40.438 7463-7463/com.coffeeloversupportlibrary E/PY: onresum:loadDaily!
    }
}



